package com.example.joker.abao00;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Menu;
import android.view.ViewConfiguration;
import android.view.Window;

import com.astuetz.PagerSlidingTabStrip;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by Joker on 2015/12/23.
 */
public class MainActivity extends FragmentActivity {

    private ChatFragment chatFragment;

    private ToolsFragment toolsFragment;

    private SettingFragment settingFragment;



    DisplayMetrics dm;

    PagerSlidingTabStrip tabs;

    ViewPager pager;

    @Override
    protected void onCreate(Bundle savaInstanceState) {
        super.onCreate(savaInstanceState);
        setContentView(R.layout.activity_main);
        setOverflowShowingalways();
        dm = getResources().getDisplayMetrics();
        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new myPagerAdapter(getSupportFragmentManager()));
        tabs.setViewPager(pager);
        setTabsValue();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    private void setTabsValue() {

        tabs.setShouldExpand(true);

        tabs.setDividerColor(Color.TRANSPARENT);
        /**
         * 将原方法注释掉,使得UnderLine移到tab的上方,
         *原方法: canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);
         *新方法: canvas.drawRect(0,0,tabsContainer.getWidth(),underlineHeight,rectPaint);
         */
        tabs.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 2, dm));

        tabs.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 4, dm));

        tabs.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, dm));

        tabs.setIndicatorColor(Color.parseColor("#648883"));

        tabs.setSelectedTextColor(Color.parseColor("#648883"));

        tabs.setTabBackground(0);
    }

    public class myPagerAdapter extends FragmentPagerAdapter {

        public myPagerAdapter(FragmentManager fm) {
            /**
             * 按住ctrl super(fm) 查看具体实现: 构造方法作用用于接收对象并,给申明的引用赋值 this.mFragmentManager = fm;
             */
            super(fm);
        }

        private final String[] title={"聊天","工具","设置"};
        @Override
        public int getCount(){
            return title.length;
        }

        @Override
        public Fragment getItem(int position){
            switch (position){
                case 0:{
                    if(chatFragment==null){
                        chatFragment=new ChatFragment();
                    }
                    return chatFragment;
                }
                case 1:{
                    if(toolsFragment==null){
                        toolsFragment=new ToolsFragment();
                    }
                    return toolsFragment;
                }
                case 2:{
                    if (settingFragment==null){
                        settingFragment=new SettingFragment();
                    }
                    return settingFragment;
                }
                default:{
                    return null;
                }
            }
        }
        @Override
        public CharSequence getPageTitle(int position){
            return title[position];
        }


    }


    @Override
    public boolean onMenuOpened(int featrueId,Menu menu){
        try{
            if(featrueId== Window.FEATURE_ACTION_BAR&&menu!=null){
                Method showIconMethod=menu.getClass().getDeclaredMethod("setOptionalIconVisible");
                showIconMethod.setAccessible(true);
                showIconMethod.invoke(menu,true);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }



    private void setOverflowShowingalways(){

        try{
            ViewConfiguration vConfig=ViewConfiguration.get(this);
            Field menuKeyField=vConfig.getClass().getDeclaredField("sHasPermanentMenuKey");
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(vConfig,false);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
